package com.dragon.common.spring.openapi.customizers.openapi;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.stereotype.Component;

/**
 * @author Chris Chan
 * Create On 2025/8/31 下午7:28
 * Use for: 多文件上传处理
 * Explain:
 * 没起作用
 */
@Component
public class MultipartFileOpenApiCustomizer implements OpenApiCustomizer {
    private static final Logger log = LoggerFactory.getLogger(MultipartFileOpenApiCustomizer.class);
    @Override
    public void customise(OpenAPI openApi) {
        log.info("MultipartFileOpenApiCustomizer~~");
        openApi.getPaths().forEach((path, pathItem) -> {
            if (path.contains("upload-multiple")) {
                Operation post = pathItem.getPost();
                if (post != null) {
                    RequestBody requestBody = post.getRequestBody();
                    if (requestBody != null) {
                        Content content = requestBody.getContent();
                        if (content != null && content.containsKey("multipart/form-data")) {
                            MediaType mediaType = content.get("multipart/form-data");
                            Schema<?> schema = mediaType.getSchema();
                            if (schema != null) {
                                // 确保 files 字段是数组类型
                                Schema<?> filesSchema = (Schema<?>) schema.getProperties().get("files");
                                if (filesSchema instanceof ArraySchema) {
                                    filesSchema.setMaxItems(10); // 设置最大文件数
                                }
                            }
                        }
                    }
                }
            }
        });
    }
}
